﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Templete
{
    public partial class ModuleSelect : BasePage
    {
        private Sys_ModuleBLL smb = new Sys_ModuleBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session.Remove("RowsPerPagex");
                Session["RowsPerPagex"] = "0";
                BindModule();
                if (Session["ModuleBufferMemory1"] != null)
                {
                    txtName.Value = Session["ModuleBufferMemory1"].ToString();
                }
                if (Session["ModuleBufferMemory2"] != null)
                {
                    ddlState.SelectedValue = Session["ModuleBufferMemory2"].ToString();
                }
                //if (Session["ModuleBufferMemory3"] != null)
                //{
                //    ModuleGroup.SelectedValue = Session["ModuleBufferMemory3"].ToString();
                //}
                if (Session["ModuleBufferMemory4"] != null)
                {
                    ddlModule.SelectedValue = Session["ModuleBufferMemory4"].ToString();
                }

                ViewState["SortOrder"] = "MODULE_ID";
                ViewState["OrderDire"] = " asc ";

                Bind();
            }

            if (SingleState.Value.ToString().Trim() == "sel")
            {
                SELECT();
                SingleState.Value = "";
            }
            if (SingleState.Value.ToString().Trim() == "upd")
            {
                UPDATE();
                SingleState.Value = "";
            }
            if (SingleState.Value.ToString().Trim() == "del")
            {
                DELETE();
                SingleState.Value = "";
            }
        }

        private void BindModule()
        {
            DataTable dataModul = smb.GetAllInfo("");
            PublicMethod.BindDropDownList(ddlModule, dataModul, "MODULE_ID", "MODULE_NAME", "MODULE_PARENTID", "-1", true);
        }

        private void Bind()
        {
            string whereStr = "";

            if (!string.IsNullOrEmpty(txtName.Value.ToString().Trim()))
            {
                whereStr += @" and  MODULE_NAME like '%" + txtName.Value.ToString().Trim() + "%' ";
            }

            if (ddlState.SelectedValue != "-1")
            {
                whereStr += @" AND MODULE_STATE=" + ddlState.SelectedValue;
            }
            if (ddlModule.SelectedValue != "-1")
            {
                whereStr += @" AND MODULE_PARENTID=" + ddlModule.SelectedValue;
            }
            ViewState["Where"] = whereStr;
            BindData(true);
        }

        private void BindData(bool isFirst)
        {
            string Where = "";
            try
            {
                Where = ViewState["Where"].ToString();
            }
            catch
            {
                Where = "";
            }

            int rowcount = 0;
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
            }
            rowcount = smb.GetRecordCount(Where, "-1");

            DataTable dtItem;

            if (PageIng.RowsPerPage == 0)
            {
                PageIng.RowsPerPage = Int32.Parse(Session["RowsPerPagex"].ToString());
            }
            else
            {
                Session["RowsPerPagex"] = PageIng.RowsPerPage.ToString();
            }

            string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];

            dtItem = smb.GetListByPage(Where, "-1", sort, PageIng.CurrentPage, PageIng.RowsPerPage);

            if (dtItem != null && dtItem.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = rowcount;
                PageIng.DataSource = dtItem;
                PageIng.GridViewSource = grvShow;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dtItem;
                PageIng.GridViewSource = grvShow;
                grvShow.DataSource = null;
                grvShow.DataBind();
                PageIng.Visible = false;
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            Session["ModuleBufferMemory1"] = txtName.Value;
            Session["ModuleBufferMemory2"] = ddlState.SelectedValue.ToString();
            //   Session["ModuleBufferMemory3"] = ModuleGroup.SelectedValue.ToString();
            Session["ModuleBufferMemory4"] = ddlModule.SelectedValue.ToString();

            MessageBox.ResponseScript(this, "FORM_INSERT();");
            // Response.Redirect("Add_Module.aspx");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DeleteClick(object sender, EventArgs e)
        {
            DELETE();
            //    Response.Redirect("SelectModule.aspx");
        }

        private void SELECT()
        {
            Bind();
        }

        private void UPDATE()
        {
            string ids = "";
            for (int i = 0; i < grvShow.Rows.Count; i++)
            {
                if (((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    ids = ((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).ToolTip;
                }
            }

            if (!string.IsNullOrEmpty(ids))
            {
                Session["ModuleBufferMemory1"] = txtName.Value;
                Session["ModuleBufferMemory2"] = ddlState.SelectedValue.ToString();
                //  Session["ModuleBufferMemory3"] = ModuleGroup.SelectedValue.ToString();
                Session["ModuleBufferMemory4"] = ddlModule.SelectedValue.ToString();

                MessageBox.ResponseScript(this, "FORM_EDIT(\"" + ids + "\");");
                //Response.Redirect("Upd_Module.aspx?ID=" + ids);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        private void DELETE()
        {
            string ids = "";
            for (int i = 0; i < grvShow.Rows.Count; i++)
            {
                if (((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    ids += ((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).ToolTip + ",";
                }
            }
            if (ids.Length > 1)
            {
                ids = ids.Substring(0, ids.Length - 1);
                int count = smb.DELETE(ids, lognUser.USER_NAME);
                if (count > 0)
                {
                    Bind();
                    MessageBox.Show(Page, "删除成功");
                }
                else
                {
                    MessageBox.Show(Page, "删除失败");
                    return;
                }
            }
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        protected void ModuleGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindModule();
        }

        /// <summary>
        /// GridView标题排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvShow_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            BindData(true);
        }
    }
}